package com.AEWMS.Servlet;

import com.AEWMS.Service.AdminService;
import com.AEWMS.Service.ParentService;
import com.AEWMS.Service.StudentService;
import com.alibaba.fastjson.JSON;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author ths
 * @create 2021/4/5 0:05
 */
@WebServlet(name = "FindBackPasswordServlet", urlPatterns = "/FindBackPasswordServlet")
public class FindBackPasswordServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String userName = request.getParameter("p1");
        String passWord = request.getParameter("p2");
        String userType = request.getParameter("p3");
        System.out.println(userName+passWord+userType);
        //学生
        if(userType.equals("student")){
            StudentService ss = new StudentService();
            if (ss.updateStu(userName,passWord)){

                response.getWriter().println(JSON.toJSONString("修改成功"));
            } else {
                response.getWriter().println(JSON.toJSONString("修改失败"));
            }
            ss.closeCon();
            return;
        }
        //管理员
        if (userType.equals("admin")){
            AdminService as = new AdminService();

            if(as.updateAdm(userName,passWord)){
                response.getWriter().println(JSON.toJSONString("修改成功"));
            }else {
                response.getWriter().println(JSON.toJSONString("修改失败"));
            }
            as.closeCon();
            return;
        }
        //家长
        if (userType.equals("parent")){
            ParentService ps = new ParentService();

            if(ps.updatePa(userName,passWord)){
                response.getWriter().println(JSON.toJSONString("修改成功"));
            }else{
                response.getWriter().println(JSON.toJSONString("修改失败"));
            }
            ps.closeCon();
        }
    }
}
